import { Controller } from "../../mvc/core/Controller";
import { Global } from "../../../com/gengine/global/Global";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SCrossSpaOpen } from "../../../Message/Public/SCrossSpaOpen";
import { SpaEntranceWindow } from "../view/spa/SpaEntranceWindow";
import { CrossSpaEntranceWindow } from "../view/spa/CrossSpaEntranceWindow";
import { SpaHintButton } from "../view/spa/SpaHintButton";
import { SSpringInteractCount } from "../../../Message/Game/SSpringInteractCount";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { SpaInteractPanel } from "../view/spa/SpaInteractPanel";
import { SpaSoapTipsWin } from "../view/spa/SpaSoapTipsWin";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { DataEvent } from "../events/DataEvent";
import { SEntityId } from "../../../Message/Public/SEntityId";
import { UserPlayer } from "../scene/player/entity/UserPlayer";
import { GameProxy } from "../mvc/GameProxy";
import { SCrossSpaList } from "../../../Message/Public/SCrossSpaList";
import { SCrossSpaInfo } from "../../../Message/Public/SCrossSpaInfo";
import { CrossSpaData } from "../view/spa/data/CrossSpaData";
import { GameConst } from "../../component/gconst/GameConst";
import { SSpaOpen } from "../../../Message/Public/SSpaOpen";
import { GameMapUtil } from "../scene/map/GameMapUtil";
type int = number;
//class SpaController
    
    export  class SpaController extends Controller
    {
        constructor()
        {
            
            super();Global.stage.addEventListener(flash.events.Event.RESIZE, this.onStageResize);
            return;
        }

        /* internal  */onCrossSpaOpen(arg1: MessageBlock): void
        {
            this._isCrossSpaOpen = true;
            var loc1=arg1.messageBase as SCrossSpaOpen;
            this.crossSpaHintButton.setTime(loc1.endDt);
            this.crossSpaHintButton.show();
            this.crossSpaHintButton.addHighLightEffect();
            return;
        }

        public get spaEntrance(): SpaEntranceWindow
        {
            if (!this._spaEntrance) 
            {
                this._spaEntrance = new SpaEntranceWindow();
            }
            return this._spaEntrance;
        }

        public onSpaClose(arg1): void
        {
            this.spaHintButton.hide();
            return;
        }

        public get crossSpaEntrance(): CrossSpaEntranceWindow
        {
            if (!this._crossSpaEntrance) 
            {
                this._crossSpaEntrance = new CrossSpaEntranceWindow();
            }
            return this._crossSpaEntrance;
        }

        /* internal  */onCrossSpaHintButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.showCrossSpaEntrance();
            return;
        }

        public get spaHintButton(): SpaHintButton
        {
            if (!this._spaHintButton) 
            {
                this._spaHintButton = new SpaHintButton();
                this._spaHintButton.addEventListener(flash.events.MouseEvent.CLICK, this.onSpaHintButtonClick);
            }
            return this._spaHintButton;
        }

        /* internal  */onSpaHintButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.showSpaEntrance();
            return;
        }

        public onCrossSpaClose(arg1): void
        {
            this._isCrossSpaOpen = false;
            this.crossSpaHintButton.hide();
            return;
        }

        public get crossSpaHintButton(): SpaHintButton
        {
            if (!this._crossSpaHintButton) 
            {
                this._crossSpaHintButton = new SpaHintButton(true);
                this._crossSpaHintButton.addEventListener(flash.events.MouseEvent.CLICK, this.onCrossSpaHintButtonClick);
            }
            return this._crossSpaHintButton;
        }

        public onIteractNumChange(arg1: MessageBlock): void
        {
            this.spaInteractPanel.setNumLabel(arg1.messageBase as SSpringInteractCount);
            return;
        }

        /* internal  */onActiveAddSpaHandler(arg1: SActiveToClient): void
        {
            if (this._spaHintButton && !this._spaHintButton.isHide) 
            {
                this._spaHintButton.updateActiveToClientInfo(arg1);
            }
            if (this._crossSpaHintButton && !this._crossSpaHintButton.isHide) 
            {
                this._crossSpaHintButton.updateActiveToClientInfo(arg1);
            }
            return;
        }

        public get spaInteractPanel(): SpaInteractPanel
        {
            if (!this._spaInteractPanel) 
            {
                this._spaInteractPanel = new SpaInteractPanel();
            }
            return this._spaInteractPanel;
        }

        public onStageResize(arg1: Object /* flash.events.Event */=null): void
        {
            if (this._spaInteractPanel && !this._spaInteractPanel.isHide) 
            {
                this._spaInteractPanel.x = Global.stage.stageWidth - 210 - 250;
                this._spaInteractPanel.y = Global.stage.stageHeight - 200;
            }
            return;
        }

        public get spaSoapTipsWin(): SpaSoapTipsWin
        {
            if (!this._spaSoapTipsWin) 
            {
                this._spaSoapTipsWin = new SpaSoapTipsWin();
            }
            return this._spaSoapTipsWin;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.SpaShowSpaEntrance, this.onShowSpaEntrance);
            Dispatcher.addEventListener(EventName.SpaEnterSpa, this.doEnterSpa);
            Dispatcher.addEventListener(EventName.SpaEnterCrossSpa, this.doEnterCrossSpa);
            Dispatcher.addEventListener(EventName.SpaLeaveSpa, this.doLeaveSpa);
            Dispatcher.addEventListener(EventName.SpaBeginSpaInteract, this.onBeginSpaInteract);
            Dispatcher.addEventListener(EventName.SpaRefreshSpaList, this.doRefreshSpaList);
            Dispatcher.addEventListener(EventName.SpaRefreshCrossSpaList, this.doRefreshCrossSpaList);
            Dispatcher.addEventListener(EventName.SpaShowLackSoapTipsWin, this.onShowLackSoapTipsWin);
            NetDispatcher.addCmdListener(ServerCommand.SpaGetSpaList, this.onGetSpaList);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossSpaList, this.onGetCrossSpaList);
            NetDispatcher.addCmdListener(ServerCommand.SpaEnterSpaMap, this.onEnterSpaMap);
            NetDispatcher.addCmdListener(ServerCommand.SpaEnterCrossSpaMap, this.onEnterCrossSpaMap);
            NetDispatcher.addCmdListener(ServerCommand.SpaLeaveSpaMap, this.onLeaveSpaMap);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicSpaOpen, this.onSpaOpen);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossSpaOpen, this.onCrossSpaOpen);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicSpaClose, this.onSpaClose);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossSpaClose, this.onCrossSpaClose);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateSpringInteractCount, this.onIteractNumChange);
            NetDispatcher.addCmdListener(ServerCommand.ActiveAddSpa, this.onActiveAddSpaHandler);
            return;
        }

        /* internal  */onShowSpaEntrance(arg1: DataEvent): void
        {
            this.showSpaEntrance();
            return;
        }

        /* internal  */doEnterSpa(arg1: DataEvent): void
        {
            this.enterSpa(arg1.data as int);
            return;
        }

        /* internal  */doEnterCrossSpa(arg1: DataEvent): void
        {
            this.enterCrossSpa(arg1.data as SEntityId);
            return;
        }

        /* internal  */doLeaveSpa(arg1: DataEvent): void
        {
            this.leaveSpa();
            return;
        }

        /* internal  */doRefreshSpaList(arg1: DataEvent): void
        {
            this.getSpaList();
            return;
        }

        /* internal  */doRefreshCrossSpaList(arg1: DataEvent): void
        {
            this.getCrossSpaList();
            return;
        }

        /* internal  */onShowLackSoapTipsWin(arg1: DataEvent): void
        {
            this.spaSoapTipsWin.show();
            return;
        }

        /* internal  */onBeginSpaInteract(arg1: DataEvent): void
        {
            var loc1=arg1.data as UserPlayer;
            if (loc1.entityInfo.entityInfo.entityId) 
            {
                GameProxy.spa.springInteract(loc1.entityInfo.entityInfo.entityId, cache.spa.interactType);
                this.spaInteractPanel.setLastClickTime();
            }
            return;
        }

        public showSpaEntrance(): void
        {
            if (this.spaEntrance.isHide && !cache.spa.isInSpa) 
            {
                this.spaEntrance.show();
            }
            else 
            {
                this.spaEntrance.hide();
            }
            return;
        }

        public showCrossSpaEntrance(): void
        {
            if (this.crossSpaEntrance.isHide && !cache.spa.isInSpa) 
            {
                this.crossSpaEntrance.show();
            }
            else 
            {
                this.crossSpaEntrance.hide();
            }
            return;
        }

        public enterSpa(arg1: int): void
        {
            GameProxy.copy.enterSpa(arg1);
            return;
        }

        public enterCrossSpa(arg1: SEntityId): void
        {
            GameProxy.copy.enterCrossSpa(arg1);
            return;
        }

        public leaveSpa(): void
        {
            GameProxy.copy.leftGroup_async();
            return;
        }

        public getSpaList(): void
        {
            GameProxy.copy.getSpaList();
            return;
        }

        public getCrossSpaList(): void
        {
            GameProxy.copy.getCrossSpaList();
            return;
        }

        /* internal  */onGetSpaList(arg1: Array<any>): void
        {
            this.spaEntrance.updateSpaList(arg1);
            return;
        }

        /* internal  */onGetCrossSpaList(arg1: MessageBlock): void
        {
            var loc4=null;
            var loc5=null;
            var loc1=arg1.messageBase as SCrossSpaList;
            var loc2=new Array<any>();
            var loc3=0;
            while (loc3 < loc1.crossSpas.length) 
            {
                loc4 = loc1.crossSpas[loc3] as SCrossSpaInfo;
                (loc5 = new CrossSpaData()).index = loc3 + 1;
                loc5.maxPlayerNum = GameConst.CrossSpaMaxPlayerNum;
                loc5.playerNum = loc4.playerNum;
                loc5.crossSpaId = loc4.spaId;
                loc2.push(loc5);
                ++loc3;
            }
            this.crossSpaEntrance.updateList(loc2);
            return;
        }

        /* internal  */onEnterSpaMap(arg1: int): void
        {
            if (this._spaEntrance && !this._spaEntrance.isHide) 
            {
                this._spaEntrance.hide();
            }
            this.spaInteractPanel.show();
            this.spaHintButton.removeHighLightEffect();
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
            this.onStageResize();
            return;
        }

        /* internal  */onEnterCrossSpaMap(arg1: int): void
        {
            if (this._crossSpaEntrance && !this._crossSpaEntrance.isHide) 
            {
                this._crossSpaEntrance.hide();
            }
            this.spaInteractPanel.show();
            this.crossSpaHintButton.removeHighLightEffect();
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
            this.onStageResize();
            return;
        }

        /* internal  */onLeaveSpaMap(arg1: Object): void
        {
            this.spaInteractPanel.hide();
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, true));
            return;
        }

        /* internal  */onSpaOpen(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SSpaOpen;
            this.spaHintButton.setTime(loc1.endDt);
            this.spaHintButton.show();
            if (cache.role.entityInfo.level >= 30 && !this._isCrossSpaOpen) 
            {
                this.spaHintButton.addHighLightEffect();
                if (!(cache.copy.isInCopy || GameMapUtil.curMapState.isArena || GameMapUtil.curMapState.isGuildMap)) 
                {
                    this.spaEntrance.show();
                }
            }
            return;
        }

        private /* var */_spaEntrance: SpaEntranceWindow;

        private /* var */_crossSpaEntrance: CrossSpaEntranceWindow;

        private /* var */_crossSpaHintButton: SpaHintButton;

        private /* var */_spaHintButton: SpaHintButton;

        private /* var */_spaInteractPanel: SpaInteractPanel;

        private /* var */_spaSoapTipsWin: SpaSoapTipsWin;

        private /* var */_isCrossSpaOpen: Boolean;
    }
